<template>
 <layout>
    <uv-sticky
      bg-color="#F5F5F5"
      offset-top="0"
      customNavHeight="0"
    >
      <uv-navbar
        :fixed="false"
        :safeAreaInsetTop="true"
        height="0"
        bgColor="transparent"
        leftIcon=""
      />
      <!-- #ifndef MP -->
   <!--  <blank size="10"></blank> -->
      <!-- #endif -->

    </uv-sticky>

	<view class="container">
			<view class="banner">
				<uv-swiper class="bg" height="330" imgMode="aspectFill" keyName="image" :list="listAds" indicatorMode="dot" indicatorStyle="bottom"></uv-swiper>
				<view class="intro">
					<view class="greet">您好，{{ isLogin ? member.nickname : '游客' }}</view>
					<view class="note">java-springboot-意向点餐外卖系统</view>
				</view>
			</view>
			<view class="content">
				<view  class="flex justify-between align-center p-2 content-user">
					<view v-if="isLogin" class="flex align-center">
						<image :src="member.avatar ? member.avatar : '/static/images/mine/default.png'" class="rounded-circle" style="width:50rpx;height: 50rpx;"></image>
						<view  class="content-user-text ml-1">{{ member.nickname }}</view>	
					</view>
					<view v-else class="content-user-text">为给您提供更好的服务清授权登录</view>				
					<view v-if="isLogin" class="py-1 px-2 content-user-login" style="color: #ffffff;">
						{{ member.cardId > 0 ? member.cardName : '普通会员' }}
					</view>
					<view v-else class="bg-white py-1 px-2 content-user-login" @tap="login">登录/注册</view>
				</view>
				<view class="entrance">
					<view class="item " @tap="takein">
						<view>
							<view class="font-md">自取</view>
							<view class="font-small text-light-black flex-1">下单免排队</view>
						</view>
						<view class="index-img">
							<image src="/static/images/index002.png" mode="aspectFit" class="img-01"></image>
						</view>	
					</view>
					<view  class="item" @tap="takeout">
						<view>
							<view class="font-md">外卖</view>
							<view class="font-small text-light-black">美食送到家</view>
						</view>
						<view class="index-img"><image src="/static/images/index003.png" mode="aspectFit" class="img-02"></image></view>
						
					</view>
				</view>
				<view class="info">
					<view class="integral_section" @tap="goScan">
						<view class="top">
							<text class="title">扫码点餐</text>
						</view>
						<view class="bottom">
							微信打开扫一扫即可享用美食哦！
						</view>
					</view>
					<view class="qrcode_section" @tap="goScan">
						<image src="/static/images/scan.png"></image>
						<text>扫一扫</text>
					</view>
				</view>
				<view class="info">
					<view class="integral_section" @tap="goScore">
						<view class="top">
							<text class="title">我的积分</text>
							<text class="value">{{member.integral}}</text>
						</view>
						<view class="bottom">
							进入积分商城兑换奈雪券及周边好礼
							<view class="iconfont iconarrow-right"></view>
						</view>
					</view>
				</view>
				<view class="navigators">
					<view class="left">
						<view class="grid flex-column just-content-center" @tap="selectShop">
							<view class="d-flex align-items-center">
								<view class="font-size-sm text-color-base">查看门店</view>
							</view>
							<view class="text-color-assist" style="margin-left: 40rpx; font-size: 20rpx;">等你发现更多线下门店</view>
						</view>
						<view class="grid justify-content-end align-items-end">
							<image src="/static/images/index004.png" style="height: 122.96rpx;width: 214.86rpx;"  mode="heightFix"></image>
						</view>
					</view>
					<view class="right">
						<view class="member-gifts" @tap="coupons">
							<view>去领个券</view>
							<view class="text-color-assist" style="margin-left: 40rpx; font-size: 20rpx;">可用于抵扣商品价格哦</view>
							<view class="right-img">
								<image src="/static/images/index006.png" style="width: 93rpx;height: 84rpx;" mode="widthFix"></image>
							</view>
						</view>
						<view class="tea-activity" @tap="invite">
							<view>邀请有礼</view>
							<view class="text-color-assist" style="margin-left: 40rpx; font-size: 20rpx;">邀请新用户领取大礼包</view>
							<view class="right-img">
								<image src="/static/images/index005.png" style="width: 93rpx;height: 84rpx;"  mode="widthFix"></image>
							</view>
						</view>
					</view>
				</view>
				
			</view>
	</view>
 </layout>
</template>

<script setup>
import {
  ref
} from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import {
  menuAds
} from '@/api/market'
import {
  getWechatConfig
} from '@/api/order'
import { storeToRefs } from 'pinia'
import { useMainStore } from '@/store/store'
// #ifdef H5
import * as jweixin from 'weixin-js-sdk'
// #endif
//
const main = useMainStore()
const { member,store, isLogin} = storeToRefs(main)
//const store = ref(main.store)
const listAds = ref([])
// const isLogin = ref(main.isLogin)

const handGetListAds = async () => {
	let shop_id = store.id ? store.id : 0;
	let data = await menuAds({
		shop_id: shop_id
	});
	if (data) {
		listAds.value = data.list;
		console.log('listAds:',listAds.value)
		uni.setStorage({
				key: 'isActive',
				data: data.isActive
			});
		if(data.list.length > 0){
			uni.setStorage({
					key: 'shopAd',
					data: data.list[0].image
			 });
			}
		}
}

const takein = () => {
	main.SET_ORDER_TYPE('takein')
	main.DEL_DESK()
	uni.switchTab({
		url: '/pages/menu/menu'
	})
}

const takeout = () => {
	main.SET_ORDER_TYPE('takeout')
	main.DEL_DESK()
	uni.switchTab({
		url: '/pages/menu/menu'
	}) 
}

const selectShop = () => {
	uni.navigateTo({
		url: '/pages/components/pages/shop/shop'
	})
}

const coupons = () => { 
	console.log("--> % orderType:\n", main.orderType)
	console.log("--> % isLogin:\n", main.isLogin)
	if(!main.isLogin) {
		uni.navigateTo({url: '/pages/components/pages/login/login'})
		return
	}
	uni.navigateTo({
		url: '/pages/components/pages/coupons/coupons?current=4'
	})
}

const goScore = () => { 
	uni.navigateTo({
		url: '/pages/components/pages/scoreproduct/list'
	})
}

const goScan = async() => {
// #ifdef  MP-WEIXIN
	uni.scanCode({
		success: function (res) {
			console.log('条码类型：',res.scanType);
			console.log('条码内容：',res.path);
			uni.navigateTo({
				url: '/' + res.path
			})
		},
		fail: function (res) {
			console.log('扫码失败：', res);
		}
	});
	// #endif
	
	// #ifdef  H5
	let config = await getWechatConfig();
	jweixin.config({
		 debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
	      appId: config.appId, // 必填，公众号的唯一标识
	      timestamp: config.timestamp, // 必填，生成签名的时间戳
	      nonceStr: config.nonceStr, // 必填，生成签名的随机串
	      signature: config.signature, // 必填，签名
	      jsApiList: [
	        'scanQRCode'
	      ] ,
	 }
	);
	jweixin.ready(function() {
		jweixin.scanQRCode({
			 needResult: 1,
			 success: function(res) {
			  console.log('wx.scanQRCode', res)
			  //alert(res.resultStr)
			  location.href = res.resultStr
			 }
		})
		
	})
	
	// #endif
}

const invite = () => {
	uni.showToast({
		title: '开发中......',
		icon: 'error'
	})
}

const login = () => {
	uni.navigateTo({
		url: '/pages/components/pages/login/login'
	})
}




onLoad(() => {
 // main.init()
  handGetListAds()
})

</script>

<style lang="scss">
.index-img {
	width: 120px;
	height: 120rpx;
	position: relative;
	left:-20rpx
}
.img-01 {
	width: 100px;
	height: 100rpx;
	margin-top: 30rpx;
}
.img-02 {
	width: 110px;
	height: 110rpx;
	margin-top: 20rpx;
}
	
	
/* #ifdef H5 */
page {
	height: auto;
	min-height: 100%;
}
/* #endif */
page {
	//background-color: #ffffff!important;
}
.banner {
	position: relative;
	width: 100%;
	//height: 300rpx;
	
	.bg {
		width: 100%;
		height: 330rpx;
	}
	
	.intro {
		position: absolute;
		top: calc(50rpx + var(--status-bar-height));
		left: 40rpx;
		color: #FFFFFF;
		display: flex;
		flex-direction: column;
		
		.greet {
			font-size: $font-size-lg;
			margin-bottom: 10rpx;
		}
		
		.note {
			font-size: $font-size-sm;
		}
	}
}

.content {
	padding: 0 20rpx;
}
.content-user {
	background-color: #ffcc00;position: relative;top:-42rpx;
}
.content-user-text {
	font-size: 24rpx;color: #ffffff;
}
.content-user-login {
	color:#ffcc00;border-radius: 40rpx;font-size: 26rpx;
}

.entrance {
	position: relative;
	//top:0;
	margin-top: -42rpx;
	margin-bottom: 20rpx;
	//border-radius: 10rpx;
	background-color: #ffffff;
	box-shadow: $box-shadow;
	padding: 30rpx 0;
	display: flex;
	align-items: center;
	justify-content: center;
	//border-radius: 50rpx;
	.item {
		//flex: 1;
		display: flex;
		//flex-direction: column;
		justify-content: center;
		//align-items: center;
		position: relative;
		
		&:nth-child(1):after {
			content: '';
			position: absolute;
			width: 1rpx;
			background-color: #ddd;
			right: 0;
			height: 100%;
			transform: scaleX(0.5) scaleY(0.8);
		}
		
		.icon {
			font-size: 90rpx;
			// color: #5A5B5C;
			color: #ffcc00;
		}
		
		.title {
			font-size: 30rpx;
			color: $text-color-base;
			font-weight: 600;
		}
	}
}

.info {
	position: relative;
	margin-bottom: 20rpx;
	border-radius: 10rpx;
	background-color: #ffffff;
	box-shadow: $box-shadow;
	padding: 30rpx;
	display: flex;
	align-items: center;
	justify-content: center;
	
	.integral_section {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
		
		.top {
			display: flex;
			align-items: center;
			
			.title {
				color: $text-color-base;
				font-size: $font-size-base;
				margin-right: 10rpx;
			}
			.value {
				font-size: 44rpx;
				font-weight: bold;
			}
		}
		
		.bottom {
			font-size: $font-size-sm;
			color: $text-color-assist;
			display: flex;
			align-items: center;
		}
	}
	
	.qrcode_section {
		color: $color-primary;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		font-size: $font-size-sm;
		
		image {
			width: 40rpx;
			height: 40rpx;
			margin-bottom: 10rpx;
		}
	}
}


.navigators {
	width: 100%;
	margin-bottom: 20rpx;	
	border-radius: 10rpx;
	background-color: #ffffff;
	box-shadow: $box-shadow;
	padding: 20rpx;
	display: flex;
	align-items: stretch;
	
	.left {
		width: 340rpx;
		margin-right: 20rpx;
		display: flex;
		padding: 0 20rpx;
		flex-direction: column;
		font-size: $font-size-sm;
		color: $text-color-base;
		background-color: #F2F2E6;
		
		.grid {
			height: 50%;
			display: flex;
		}
	}
	
	.right {
		width: 290rpx;
		display: flex;
		flex-direction: column;
		
		.tea-activity, .member-gifts {
			width: 100%;
			display: flex;
			padding: 20rpx;
			font-size: $font-size-sm;
			color: $text-color-base;
			align-items: center;
			position: relative;
		}
		
		.tea-activity {
			background-color: #FDF3F2;
		}
		
		.member-gifts {
			background-color: #FCF6D4;
			margin-bottom: 20rpx;
		}
		
		.right-img {
			flex: 1;
			position: relative;
			margin-left: 20rpx;
			margin-right: -70rpx;
			margin-bottom: -20rpx;
			display: flex;
			align-items: flex-end;
			
			image {
				width: 100%;
			}
		}
	}
	
	

}


</style>
